package com.wbsxch.secondkill.web;

import com.wbsxch.secondkill.dto.Exposer;
import com.wbsxch.secondkill.dto.SecondKillExecution;
import com.wbsxch.secondkill.dto.SecondKillResult;
import com.wbsxch.secondkill.entity.SecondKill;
import com.wbsxch.secondkill.enums.SecondKillStateEnum;
import com.wbsxch.secondkill.exception.RepeatKillException;
import com.wbsxch.secondkill.exception.SecondKillCloseException;
import com.wbsxch.secondkill.service.SecondKillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/secondKill")
public class SecondKillController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SecondKillService secondKillService;

    @GetMapping(name = "/list")
    public String list(Model model) {
        List<SecondKill> secondKills = secondKillService.selectAll();
        model.addAttribute("list", secondKills);
        return "list";
    }

    @GetMapping(name = "/{secondKillId}/detail")
    public String detail(@PathVariable Long secondKillId, Model model) {
        if(secondKillId == null){
            return "redirect:/secondKill/list";
        }
        SecondKill secondKill = secondKillService.selectById(secondKillId);
        if(secondKill == null){
            return "forward:/secondKill/list";
        }
        model.addAttribute("secondKill",secondKill);
        return "detail";
    }

    @PostMapping(name = "{/{secondkillId}/exposer",produces = {"application/json,charset=UTF-8"})
    @ResponseBody
    public SecondKillResult<Exposer> exposer(@PathVariable Long secondKillId){
        SecondKillResult<Exposer> result;
        try {
            Exposer exposer = secondKillService.exportSecondKillUrl(secondKillId);
            result = new SecondKillResult<>(true,exposer);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            result = new SecondKillResult<>(false,e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/{secondKillId}/{md5}/execution",produces = "application/json,charset=UTF-8")
    @ResponseBody
    public SecondKillResult<SecondKillExecution> execute(@PathVariable("secondKillId") Long secondKillId,@PathVariable("md5") String md5,@CookieValue(name = "phone")Long phone){
        SecondKillResult<SecondKillExecution> result;
        if(phone == null){
            result = new SecondKillResult<>(false,"未验证");
            return result;
        }
        try {
            SecondKillExecution secondKillExecution = secondKillService.executeSecondKill(secondKillId, phone, md5);
            result = new SecondKillResult<>(true,secondKillExecution);
        }catch (RepeatKillException e1){
            SecondKillExecution secondKillExecution = new SecondKillExecution(secondKillId,SecondKillStateEnum.REPEAT_KILl);
            result = new SecondKillResult<>(true,secondKillExecution);
        }catch (SecondKillCloseException e2){
            SecondKillExecution secondKillExecution = new SecondKillExecution(secondKillId,SecondKillStateEnum.END);
            result = new SecondKillResult<>(true,secondKillExecution);
        }catch(Exception e){
            SecondKillExecution secondKillExecution = new SecondKillExecution(secondKillId,SecondKillStateEnum.INNER_ERROR);
            result = new SecondKillResult<>(true,secondKillExecution);
        }
        return result;
    }

    @GetMapping("/time/now")
    public SecondKillResult<Long> time(){
        return new SecondKillResult<>(true,System.currentTimeMillis());
    }

}
